package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Business;
import Database.DatabaseConnector;

@WebServlet("/businessServlet")
public class businessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		DatabaseConnector dbConnect = new DatabaseConnector();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		String busName = (String) session.getAttribute("username");
		
		
		Business profile = dbConnect.getProfile(busName);
	
		JSONObject result = new JSONObject();
		try {
			if (profile != null) {
				result.put("business", profile.getBusname());
				result.put("telephone", profile.getBusNum());
				result.put("cell", profile.getCell());
				result.put("city", profile.getCity());
				result.put("email", profile.getEmail());
				result.put("fax", profile.getFax());
				result.put("postal", profile.getPostal());
				result.put("province", profile.getProvince());
				result.put("address", profile.getStreetAd());
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		res.getWriter().print(result);
		res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
